<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <router-link :to="{ name: 'GdSchoolAdd' }">
        <a-button type="primary" icon="plus">新建</a-button>
      </router-link>
    </div>

    <a-table
      ref="table"
      :row-key="
        (record) => {
          return record.schoolId
        }
      "
      :columns="columns"
      :data-source="data"
      :loading="loading"
      :pagination="pagination"
      @change="onChange"
    >
      <span slot="status" slot-scope="text, record">
        <a-switch
          checked-children="上架"
          un-checked-children="下架"
          @change="
            (checked) => {
              changeStatus(record.schoolId, checked)
            }
          "
          :checked="record.status == 1 ? true : false"
        />
      </span>
      <span slot="action" slot-scope="text, record">
        <router-link :to="{ name: 'GdSchoolEdit', params: { id: record.schoolId } }">修改</router-link>
        <a-divider type="vertical" />
        <a @click="deleteOne(record.schoolId)">删除</a>
      </span>
    </a-table>
  </a-card>
</template>
<script>
const columns = [
  {
    title: 'ID',
    dataIndex: 'schoolId'
  },
  {
    title: '学校名称',
    dataIndex: 'schoolName'
  },
  {
    title: '活动名称',
    dataIndex: 'subject'
  },
  {
    title: '操作',
    key: 'action',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'SchoolList',
  data() {
    return {
      id: '',
      columns,
      loading: false,
      data: [],
      pagination: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultCurrent: 1, // 默认当前页数
        defaultPageSize: 10, // 默认当前页显示数据的大小
        total: 0, // 总数，必须先有
        pageSizeOptions: ['10', '20', '30', '50', '100'],
        showTotal: (total, range) => {
          return (
            <div>
              共 <span>{total}</span> 条
            </div>
          )
        },
        onShowSizeChange: (current, pageSize) => {
          this.pageSize = pageSize
        }
      },
      queryParam: {},
      actives: []
    }
  },
  mounted() {
    // 获取列表
    this.fetch()
  },
  methods: {
    // 开始时间
    onStartDateChange(data, dataString) {
      this.queryParam.startTime = dataString
    },
    // 结束时间
    onEndDateChange(data, dataString) {
      this.queryParam.endTime = dataString
    },
    // 改变每页数量时更新显示
    onChange(pagination) {
      this.pagination.defaultCurrent = pagination.current
      this.pagination.defaultPageSize = pagination.pageSize
      this.fetch()
    },
    // 搜索
    searchBtn() {
      this.pagination.defaultCurrent = 1
      this.fetch()
    },
    // 动态获取列表
    fetch() {
      this.loading = true
      const params = {
        page: this.pagination.defaultCurrent,
        pagesize: this.pagination.defaultPageSize
      }
      this.$common
        .handleGet('/gdzyz/school/lists', Object.assign({}, params, this.queryParam))
        .then((data) => {
          this.loading = false
          if (data.code === 200) {
            const result = data.data
            const pagination = { ...this.pagination }
            pagination.total = result.totalInfo.total
            this.data = result.lists
            this.pagination = pagination
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    },
    // 上架、下架
    changeStatus(id, status) {
      if (id && status !== '') {
        var message = ''
        var url = ''
        if (status) {
          message = '确定要上架吗？'
          url = '/activities/enable/' + id
        } else {
          message = '确定要下架吗？'
          url = '/activities/disable/' + id
        }
        const _this = this
        this.$confirm({
          title: message,
          onOk() {
            _this.$common
              .handlePost(url, {})
              .then((data) => {
                if (data.code === 200) {
                  _this.$message.success(data.message)
                  _this.fetch()
                } else {
                  _this.$message.error(data.message)
                }
              })
              .catch(() => {
                _this.$notification.error({
                  message: '错误',
                  description: '请求信息失败，请重试'
                })
              })
          },
          onCancel() {
            console.log('Cancel')
          },
          class: 'test'
        })
      } else {
        this.$message.error('参数错误')
      }
    },
    // 删除某一个
    deleteOne(id) {
      this.handleDelete(id)
    },
    handleDelete(deleteIds) {
      const _this = this
      const params = {
        id: deleteIds
      }
      this.$common
        .deleteOne('/gdzyz/school/delete/' + deleteIds, params, deleteIds)
        .then((res) => {
          if (res) {
            _this.fetch()
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    }
  }
}
</script>
